<?php
            


/**
 * Desc:示例model 通用化 数据库模型 前后端
 * Time: 2021/3/10 10:29 上午
 */
class DemoModel extends PluginModel {

    /**
     * Desc:获取指定的列表数据总量 尽量带参数 测试示例无法调用
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2021/3/10 4:47 下午
     * @param $themeid
     * @param string $name
     * @param int $status
     * @return mixed
     */
    public static function getDemoCount($themeid, $name='', $status=0)
    {
        $condition = " and status=$status";
        if ($name) {
            $condition .= " and name='".$name."'";
        }
        $sql = " SELECT COUNT(*) as cnt FROM ".tablename(yx_demo)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }

    /**
     * Desc:获取对应条件的列表
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2021/3/10 4:47 下午
     * @param $themeid
     * @param string $name
     * @param int $status
     * @param int $page_start
     * @param int $page_size
     * @return mixed
     */
    public static function getDemoList($themeid, $name = '', $status = 0, $page_start=0, $page_size = 0)
    {
        $condition = "";
        if ($name) {
            $condition .= " and name='".$name."'";
        }
        if ($status) {
            $condition .= " and status = ".$status;
        }
        if ($page_size) {
            $condition .= " limit $page_start,".$page_size;
        }
        $sql = " SELECT * FROM ".tablename(yx_demo)." WHERE themeid=$themeid".$condition;
        return db_select($sql);
    }

    /**
     * Desc:官方推荐的列表查询
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2021/3/10 4:58 下午
     * @param $where  array 查询条件
     * @param array $fields array 需要使用到的字段   最小原则
     * @param string $keyfield string 参数传入一个已存在的字段名称，结果数组键值就为该字段，否则为自然排序
     * @param array $orderby array 按xxx升/降排序
     * @param array $limit 分页，array(当前页, 每页条页)
     */
    public static function getDemoListDefault($where, $fields = [], $keyfield = '', $orderby= [], $limit = [])
    {
        //获取全部启用的用户
        //生成的SQL等同于：SELECT `username`,`uid` FROM `yx_sys_users` WHERE `status` = 2 ORDER BY `starttime` desc,`endtime` desc  LIMIT 0, 5
        //$user = db_getall('users', array('status' => 2), array('username', 'uid'), 'uid', array('starttime desc', 'endtime desc'), array(1,5));
        $user = db_getall('yx_demo', $where, $fields, $keyfield, $orderby, $limit);
    }

    /**
     * Desc:插入数据
     * Email:17854288794@163.com
     * Time: 2021/3/10 4:49 下午
     * @param $data
     * @return false
     */
    public static function insertDemoData($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s",time());
        $res = $insertid = db_insert(yx_demo,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    /**
     * Desc:获取指定查询条件的item
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2021/3/10 4:53 下午
     * @param $where array 查询条件
     * @param array $fields  查询字段  最小原则
     * @return mixed
     */
    public static function getDemoItem($where,$fields=[])
    {
        return db_get(yx_demo,$where,$fields);
    }

    public static function getFirstDemo()
    {
        return_json(1,'chucuo');
        return ['code'=>1,'msg'=>'','data'=>[]];
    }
}